package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lenovo
 */
@Slf4j
@RestController
@RequestMapping("role")
public class RoleController {
	@DubboReference
	RoleService roleService;
/**
 * @Author: wty
 * @Date: 18:11 2021/3/11
 * @Parms []
 * @ReturnType: com.itheima.health.entity.Result
 * @Description: 查询所有角色
*/
	@GetMapping("findAllRoles")
	public Result findAllRoles() {
	List<Role> roleList= roleService.findAllRoles();
		return new Result(true, MessageConst.ACTION_SUCCESS, roleList);
	}
/**
 * @Author: wty
 * @Date: 18:12 2021/3/11
 * @Parms [queryPageBean]
 * @ReturnType: com.itheima.health.entity.Result
 * @Description:
*/
	@GetMapping("findPage")
	public Result findPage(QueryPageBean queryPageBean) {
		PageResult pageResult = roleService.findPage(queryPageBean);
		return new Result(true, MessageConst.ACTION_SUCCESS, pageResult);
	}
	/**
	 * @Author: jay
	 * @Date: 9:12 2021/3/12
	 * @Params [role, menuIds, permissionIds]
	 * @ReturnType: com.itheima.health.entity.Result
	 * @Description: 角色管理-新增
	 */
	@PostMapping("add")
	public Result add(@RequestBody Role role, Integer[] menuIds,Integer[] permissionIds){
		log.info("[角色管理-新增] data:{},menuIds:{},permissionIds:{}",role,menuIds,permissionIds);
		roleService.add(role,menuIds,permissionIds);
		return new Result(true,MessageConst.ACTION_SUCCESS);
	}

/**
 * @Author: wty
 * @Date: 18:13 2021/3/11
 * @Parms [id]
 * @ReturnType: com.itheima.health.entity.Result
 * @Description: 删除用户
*/
	@GetMapping("delete")
	public Result delete(Integer id) {
		roleService.delete(id);
		return new Result(true, MessageConst.ACTION_SUCCESS);
	}


	/**
	 * @Author: jay
	 * @Date: 10:05 2021/3/12
	 * @Params [menu, menuIds, permissionIds]
	 * @ReturnType: com.itheima.health.entity.Result
	 * @Description: 角色管理-修改
	 */
	@PostMapping("edit")
	public Result edit(@RequestBody Role role,Integer[] menuIds,Integer[] permissionIds ){
		log.info("[角色管理-修改] data:{},menuIds:{},permissionIds:{}",role,menuIds,permissionIds);
		roleService.edit(role,menuIds,permissionIds);
		return new Result(true,MessageConst.ACTION_SUCCESS);
	}

	/**
	 * @Author: jay
	 * @Date: 9:48 2021/3/12
	 * @Params [id]
	 * @ReturnType: com.itheima.health.entity.Result
	 * @Description: 角色-根据id查询菜单id
	 */
	@GetMapping("findMenuIdsByRoleId")
	public Result findMenuIdsByRoleId(Integer id){
		log.info("[角色-根据id查询菜单id] data:{}",id);
		List<Integer> menuIds = roleService.findMenuIdsByRoleId(id);
		return new Result(true, MessageConst.ACTION_SUCCESS,menuIds);
	}

	/**
	 * @Author: jay
	 * @Date: 9:54 2021/3/12
	 * @Params [id]
	 * @ReturnType: com.itheima.health.entity.Result
	 * @Description: 角色-根据id查询权限id
	 */
	@GetMapping("findPermissionIdsByRoleId")
	public Result findPermissionIdsByRoleId(Integer id){
		log.info("[角色-根据id查询权限id] data:{}",id);
		List<Integer> permissionIds = roleService.findPermissionIdsByRoleId(id);
		return new Result(true,MessageConst.ACTION_SUCCESS,permissionIds);
	}



	/**
	 * @Author: jay
	 * @Date: 20:23 2021/3/12
	 * @Params [id]
	 * @ReturnType: com.itheima.health.entity.Result
	 * @Description: 角色管理-根据id查询
	 */
	@GetMapping("findById")
	public Result findById(Integer id){
		log.info("[角色管理-根据id查询] id:{}",id);
		Role role = roleService.findByid(id);
		return new Result(true,MessageConst.ACTION_SUCCESS,role);
	}

}
